﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PseudoOriginalServer
{
    public interface IRetry
    {
        void Try(Action action);
    }

    public class RetryByCount : IRetry
    {
        public int Count { get; set; } = 1;

        public void Try(Action action)
        {
            var count = Count;
            Exception ex = null;

            while (true)
            {
                try
                {
                    ex = null;
                    action();
                    break;
                }
                catch (Exception e)
                {
                    ex = e;
                }

                if (--count <= 0)
                    break;
            }

            if (ex != null)
                throw ex;
        }
    }

    public static class Retry
    {
        public static void ByCount(Action action, int count = 1)
        {
            new RetryByCount { Count = count }.Try(action);
        }
    }
}
